package com.wgo.precise.client.ui.controller;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IElementFactory;
import org.eclipse.ui.IMemento;


import com.wgo.bpot.server.persist.Persistent;
import com.wgo.precise.client.ui.model.facade.IModelPersistable;
import com.wgo.precise.client.ui.view.editor.dummy.DummyItem;
import com.wgo.precise.client.ui.view.util.EditorInput;
import com.wgo.precise.client.ui.view.util.ReflectionHelper;

/**
 * 
 * @see org.eclipse.ui.IElementFactory
 * 
 * @author Petter L. H. Eide <petter@eide.biz>
 * @version $Id: ModelItemFactory.java,v 1.3 2006-01-31 22:40:30 petterei Exp $
 */
public class ModelItemFactory implements IElementFactory {
	
    /**
     * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
     */
    public IAdaptable createElement(IMemento memento) {
        long boxedIdentifier = memento.getFloat(Registry.MEMENTO_IDENTIFIER_ID).longValue();
        Long identifier = null;
        if (0 != boxedIdentifier) {
        	identifier = new Long(boxedIdentifier);
        }
        String title = memento.getString(Registry.MEMENTO_TITLE_ID);
        String description = memento.getString(Registry.MEMENTO_DESCRIPTION_ID);
        String typeName = memento.getString(Registry.MEMENTO_CLASS_NAME_ID);
        Class<? extends Persistent> domainObjectType = ReflectionHelper.<Persistent>toClass("Could not restore class.", typeName);
        IEditorInput input = null;
        if (RequirementPlugin.getInstance().isLoggedIn()) {
            IModelPersistable item = RequirementPlugin.getInstance().getSession().getModelItem(identifier);
            input = new EditorInput(item);
        } else {
        	input = new EditorInput(new DummyItem(identifier, domainObjectType, title, description));
        }
        if (null == input) {
        	input = new EditorInput(new DummyItem(identifier, domainObjectType, title, description));
        }
        return input;
    }

}
/**
 * 
 * Changelog:
 * 
 * $Log: ModelItemFactory.java,v $
 * Revision 1.3  2006-01-31 22:40:30  petterei
 * tuned the editor window management
 *
 * Revision 1.2  2006-01-30 21:52:33  petterei
 * implemented wrapper factory and lazy wrapper-instantiation of parents
 * implemented a "system" for each top-node type
 * added top-categories for all top-nodes
 * implemented some interfaces for the wrappers, and started using these
 *
 * Revision 1.1  2006-01-23 19:58:37  petterei
 * initial commit for client, fitting the alternative session implementation
 *
 * Revision 1.5  2006-01-13 08:12:42  petterei
 * Using Long consequently to differentiate between �null� and �0� for dbId and conceptId (no boxing/unboxing)
 * Ajusted code to stronger java-syntax
 *
 * Revision 1.4  2006-01-03 13:32:32  petterei
 * adjustments and finery
 *
 * Revision 1.3  2005-12-05 04:16:41  petterei
 * *** empty log message ***
 *
 * Revision 1.2  2005-11-16 16:43:28  petterei
 * mye testing...
 *
 * Revision 1.1  2005/10/11 15:06:59  petterei
 * Seccond iteration:
 * 1. Eclipse based client with new domain model
 * 2. Flexible Project and Category hierarchy with Drag-n-Drop functionality
 * New functionality:
 * 3. Eclipse plugin update-site
 *
 * Revision 1.1  2005/08/23 06:38:48  peide3
 * *** empty log message ***
 *
 * Revision 1.2  2005/08/09 09:23:25  petterei
 * *** empty log message ***
 *
 * Revision 1.1  2005/07/31 22:55:09  petterei
 * initial commit
 *
 */
